import BaiduCloudClientBaseConfig from './config';
export class BaiduCloudClient {
  private sdk; // 导入的baiduCloudSdk
  private init_config: InitConfigTypes //初始化时用到的 ak sk endpoint（目标URL）有默认值一般不用传;
  private params: object; //前端传给后台的params
  private body; //前端传给后台的body
  private method: string; //使用何种方式调用千帆API  post、get..... 默认post
  private path: string; //
  private headers: object; //请求千帆api时携带的header 默认content-type: applation/json
  private client: any; //baiduCloudSdk创建请求后的实例
  constructor(params: BaiduCloudClientTypes) {
    this.sdk = require('@baiducloud/sdk');
    this.init_config = {
      ak: params.init_config?.ak || BaiduCloudClientBaseConfig.ak,
      sk: params.init_config?.sk || BaiduCloudClientBaseConfig.sk,
      endpoint:
        params.init_config?.endpoint || BaiduCloudClientBaseConfig.endpoint,
    };
    this.params = params.request.args.params;
    this.body = params.request.args.body;
    this.method =
      params.request.config?.method || BaiduCloudClientBaseConfig.method;
    this.path = params.request.config.path;
    this.headers = params.request.config.headers || {
      'Content-Type': BaiduCloudClientBaseConfig.contentType,
    };
  }

  init() {
    this.client = new this.sdk.HttpClient({
      credentials: {
        ak: this.init_config.ak,
        sk: this.init_config.sk,
      },
      endpoint: this.init_config.endpoint,
    });
  }

  // eslint-disable-next-line @typescript-eslint/no-empty-function
  async sendRequest() {
    return await this.client.sendRequest(
      this.method,
      this.path,
      this.body,
      this.headers,
      this.params,
    );
  }

  getConfig() {
    return {
      config: this.init_config,
      params: this.params,
      body: this.body,
      method: this.method,
      path: this.path,
      headers: this.headers,
    };
  }
}
